package checkers.client.logic.classes;

import checkers.client.logic.classes.network.NetLayer;
import checkers.common.logic.classes.CheckersPosition;
import checkers.common.logic.classes.Point;
import checkers.common.logic.commands.AuthorizeRequest;
import checkers.common.logic.commands.AuthorizeResponse;
import checkers.common.logic.commands.Command;
import checkers.common.logic.commands.RegisterRequest;
import checkers.common.logic.commands.RegisterResponse;

public class Board {

	private int[][] positions;
	private NetLayer layer;
	private Logger log;
	
	public Board (NetLayer layer, Logger log) {
		this.layer = layer;
		this.log = log;
		positions = CheckersPosition.defaultPositions; 
	}
	
	public int[][] getPositions() {
		return positions;
	}

	public Point[] findMove(int x, int y) {
		return null;
	}

	public boolean move() {
		return true;
	}
	
	public Logger getLog() {
		return log;
	}
	
	public boolean register(String login) throws Exception {
		RegisterRequest request = new RegisterRequest();
		request.setLogin(login);
		layer.sendCommand(request);
		Command c = layer.startReceivingCommand();
		
		if (c.getId() == 3) {
			RegisterResponse rRes = (RegisterResponse)c;
			return rRes.getCheckRegister() == 1 ? true : false;
		}
		return false;
	}
	
	public boolean login(String login) throws Exception {
		AuthorizeRequest request = new AuthorizeRequest();
		request.setLogin(login);
		layer.sendCommand(request);
		Command c = layer.startReceivingCommand();
		
		if (c.getId() == 3) {
			AuthorizeResponse aRes = (AuthorizeResponse)c;
			return aRes.getCheckAuthorize() == 1 ? true : false;
		}
		return false;
	}

}
